package com.njtoyo.taxi.admin.service.business.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.netrides.entity.CompanyBank;
import com.njtoyo.taxi.admin.library.payment.cmb.CmbPayClient;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyBankMapper;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.AdminUserMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.CompanyWithdrawListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.business.driver_withdraw.DriverWithdrawListPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.AddCompanyWithdrawWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.CompanyWithdrawWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver_withdraw.IndexWrapper;
import com.njtoyo.taxi.admin.service.business.CompanyBankService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author mdf
 * @title: CompanyBankServiceImpl
 * @description: TODO
 * @date 2024/5/29 10:44
 */
@Service
public class CompanyBankServiceImpl extends ServiceImpl<CompanyBankMapper, CompanyBank> implements CompanyBankService {
    @Override
    public RestResult getByCompanyId(Long companyId) {
        CompanyBank companyBank = baseMapper.selectOne(Wrappers.<CompanyBank>lambdaQuery().eq(CompanyBank::getCompanyId, companyId));
        return RestResult.success(companyBank);
    }

    @Override
    public RestResult saveOrUpdateBank(AddCompanyWithdrawWrapper add) {
        CompanyBank companyBank = baseMapper.selectOne(Wrappers.<CompanyBank>lambdaQuery().eq(CompanyBank::getCompanyId, add.getCompanyId()));
        CompanyBank addBank = BeanUtil.copyProperties(add, CompanyBank.class);
        if (ObjectUtil.isNull(companyBank)) {
            baseMapper.insert(addBank);
        } else {
            companyBank.setBankName(add.getBankName());
            companyBank.setCardNumber(add.getCardNumber());
            companyBank.setNameOnCard(add.getNameOnCard());
            companyBank.setBankCity(add.getBankCity());
            baseMapper.updateById(companyBank);
        }
        return RestResult.success();
    }

    @Autowired
    AdminUserMapper adminUserMapper;
    @Override
    public RestResult getPage(AdminUser adminUser, CompanyWithdrawWrapper wrapper) {
        Page<CompanyWithdrawListPresenter> res = baseMapper.getPage(wrapper);
        List<CompanyWithdrawListPresenter> records = res.getRecords();
        setName(records);
        return RestResult.success(res);
    }
    private void setName(List<CompanyWithdrawListPresenter> records){
        if(CollUtil.isEmpty(records))
            return;
        Set<Integer> set = new HashSet<>();
        records.forEach(e->{
            Integer examineId = e.getExamineId();
            if(examineId != null)
                set.add(examineId);
            Integer createId = e.getCreateId();
            if(createId != null)
                set.add(createId);
        });
        if(CollUtil.isEmpty(set))
            return;
        List<AdminUser> names = adminUserMapper.selectNameList(new ArrayList<>(set));

        if(CollUtil.isEmpty(names))
            return;

        Map<Integer, String> collect = names.stream().collect(Collectors.toMap(AdminUser::getId, AdminUser::getName));
        records.forEach(e->{
            e.setCreateBy(collect.get(e.getCreateId()));
            e.setExamineBy(collect.get(e.getExamineId()));
        });
    }

    @Override
    public RestResult getCompanyBalance() {
        String result = CmbPayClient.mbrBalQry();
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getInteger("code") == 10000) {
            JSONObject data = jsonObject.getJSONObject("data");
            return RestResult.success(data.get("avaBal"));
        } else {
            return RestResult.failed("余额查询异常");
        }
    }
}
